import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:memo/pages/add_todo_page.dart';
import 'package:memo/pages/home_page.dart';
import 'package:memo/state/model/todo.dart';

class TodoItemWidget extends StatelessWidget {
  final Todo todo;
  final VoidCallback onLongPress;
  const TodoItemWidget({Key key, this.todo, this.onLongPress}): super(key: key);

  @override
  Widget build(BuildContext context) {
    return InkWell(
      child: _itemWidget(todo),
      onLongPress: onLongPress,
      onTap: () => Navigator.of(context).push(
          MaterialPageRoute(builder: (context) => AddTodoPage(todo))),
    );
  }

  Widget _itemWidget(Todo todo) {
    return Container(
      padding: EdgeInsets.only(left: 10.0, right: 10.0, top: 10.0),
      height: 160.0,
      child: Card(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            const SizedBox(height: 6.0),
            Padding(
              padding: const EdgeInsets.only(right: 10.0),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  IconButton(
                    icon: Icon(
                      Icons.brightness_1,
                      color: todo.type == TodoType.success
                          ? successColor
                          : (todo.type == TodoType.waring
                          ? waringColor
                          : errorColor),
                    ),
                    onPressed: () {},
                  ),
                  Text(
                    "${DateFormat("MM-dd HH:mm").format(todo.time)}",
                    style:
                    TextStyle(fontWeight: FontWeight.bold, fontSize: 16.0),
                  )
                ],
              ),
            ),
            const SizedBox(height: 6.0),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 12.0),
              child: Text("${todo.note}",
                  maxLines: 3, overflow: TextOverflow.ellipsis),
            ),
          ],
        ),
      ),
    );
  }
}


Future<bool> deleteDialog(BuildContext context) async {
  return showDialog<bool>(
      context: context,
      barrierDismissible: true,
      // dialog is dismissible with a tap on the barrier
      builder: (BuildContext context) {
        return AlertDialog(
          content: Text("是否删除"),
          actions: <Widget>[
            FlatButton(
              child: const Text('删除'),
              onPressed: () {
                Navigator.of(context).pop(true);
              },
            ),
            FlatButton(
              child: const Text('返回'),
              onPressed: () {
                Navigator.of(context).pop(false);
              },
            ),
          ],
        );
      });
}